<template>
  <border-box title="大数据">
    <a-input-search v-model="keyword" placeholder="请输入搜索内容" @search="loadData"></a-input-search>
    <a-spin :spinning="loading">
      <a-tabs @change="tabChange">
        <a-tab-pane key="0" tab="文本">
          <a-empty v-if="!textList||textList.length===0" description="没有找到相关内容，请换个关键词试试！"></a-empty>
          <div class="item" v-for="(item,index) in textList" :key="index">
            <div class="item-title">{{item.title}}</div>
            <div class="item-content">
              {{item.description}}
            </div>
            <div class="item-footer">
              <a-tag color="pink">{{item.source}}</a-tag>
              <div class="item-time">{{item.ctime}}</div>
              <a-button type="primary" size="small" @click="insertContent(item.url)">插入</a-button>
            </div>
          </div>
        </a-tab-pane>
        <a-tab-pane key="1" tab="图片">
          <a-empty v-if="!imgList||imgList.length===0" description="没有找到相关内容，请换个关键词试试！"></a-empty>
          <a-row type="flex">
            <a-col v-for="(item,index) in imgList" :key="index">
              <img class="item-img" :src="item.contentUrl" @click="insertImg(item.contentUrl)">
            </a-col>
          </a-row>
          <a-pagination size="small" @change="loadData" v-model="queryParams.current"
                        :total="queryParams.total"></a-pagination>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </border-box>
</template>

<script>
  //大数据
  import BorderBox from "./BorderBox";
  import {getAction} from "@/api/manage";

  export default {
    name: "BigData",
    components: {BorderBox},
    data() {
      return {
        loading: false,
        type: '0',
        keyword: '',
        textList: [],
        imgList: [],
        queryParams: {
          current: 1,
          total: 0,
          pageSize: 18
        }
      }
    },
    created() {
      this.loadData()
    },
    methods: {
      loadData() {
        if (this.type == '0') {
          this.loadTextData();
        } else {
          this.loadImageData();
        }
      },
      async loadTextData() {
        this.loading = true;
        const res = await getAction('big/data/queryNewsList', {key: this.keyword, type: this.type,});
        if (res.result) {
          this.textList = res.result;
        }
        this.loading = false;
      },
      async loadImageData() {
        this.loading = true;
        const {current, pageSize} = this.queryParams;
        const offset = (current - 1) * pageSize;
        const params = {
          key: this.keyword,
          type: this.type,
          count: 18,
          offset
        };
        const res = await getAction('big/data/queryNewsList', params);
        if (res.result) {
          this.imgList = res.result.value;
          this.queryParams.total = res.result.totalEstimatedMatches;
        }
        this.loading = false;
      },
      tabChange(key) {
        this.type = key;
        this.loadData();
      },
      insertContent(url) {
        getAction('big/data/extractUrl', {url}).then(res => {
          if (res.success) {
            if (res.result) {
              const firstChild = res.result[0];
              if (firstChild && firstChild.content) {
                this.$emit('insert', firstChild.content)
                return;
              }
            }
          }
          const key = `insert${Date.now()}`;
          this.$notification.error({
            key,
            message: '插入失败',
            description: '解析失败，点击去手动粘贴->',
            onClick: () => {
              //打开新的窗口
              window.open(url, '_blank');
              this.$notification.close(key);
            },
          });
        })
      },
      insertImg(url) {
        this.$emit('insert', '<img src="' + url + '" />')
      }
    }
  }
</script>

<style scoped lang="less">
  .item {
    padding: 10px 0;

    &-title {
      font-size: 14px;
      color: #333;
      font-weight: 700;
      margin-bottom: 5px;
    }

    &-content {
      font-size: 13px;
      line-height: 20px;
      word-break: break-all;
      margin-bottom: 5px;
    }

    &-footer {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }

    &-time {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 13px;
      margin-top: 4px;
    }

    &-img {
      width: 100px;
      height: auto;
      border-radius: 5px;
      border: 1px solid #ededed;
      margin: 5px;
      cursor: pointer;
    }
  }


</style>